﻿using AdverseEventReportSystem.BLL;
using AdverseEventReportSystem.BLL.ServiceReference1;
using AdverseEventReportSystem.Core;
using AdverseEventReportSystem.Utils;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace AdverseEventReportSystem.Web.Areas.Sysbase
{
    public class DepartmentController : BaseController
    {
        // GET: Sysbase/Department
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 部门列表
        /// </summary>
        /// <returns></returns>
        public ActionResult Search()
        {
            string departmentName = DataConvert.ToString(Request["departmentName"]);
            string departmentCode = DataConvert.ToString(Request["departmentCode"]);
            string departmentType = DataConvert.ToString(Request["departmentType"]);
            FunctionAndPermissionsSoapClient service = new FunctionAndPermissionsSoapClient();
            ResultHelperOfDataTable serviceResult = service.GetDepartmentDt(departmentName, departmentCode, departmentType);
            DataTable dt = new DataTable();
            if (serviceResult.S)
            {
                dt = serviceResult.D;
                if (dt != null)
                {
                    dt.DefaultView.Sort = "sort";
                    dt = dt.DefaultView.ToTable();
                }
            }

            var result = new { Rows = dt };
            return JsonDate(result);
        }

        /// <summary>
        /// 添加/编辑
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Edit(int id = 0)
        {
            tb_Department department = new tb_Department();
            if (id > 0)
            {
                FunctionAndPermissionsSoapClient service = new FunctionAndPermissionsSoapClient();
                ResultHelperOftb_Department result = service.GetDepById(id);
                if (result.S)
                {
                    department = result.D;
                }
            }
            ViewBag.Department = department;
            return View();
        }

        /// <summary>
        /// 添加/编辑部门
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Save(tb_Department department)
        {
            OperateResult resultInfo = DepartmentManager.Save(department);
            return JsonDate(resultInfo);
        }

        /// <summary>
        /// 删除部门
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Delete(int id)
        {
            OperateResult resultInfo = DepartmentManager.Delete(id);
            return JsonDate(resultInfo);
        }

        /// <summary>
        /// 获取所有部门
        /// </summary>
        /// <param name="isAll"></param>
        /// <param name="isAddRows"></param>
        /// <param name="departmentType"></param>
        /// <returns></returns>
        public ActionResult GetAllDepartment(int isAll = 0, int isAddRows = 0, string departmentType = "")
        {
            DataTable dt = null;
            FunctionAndPermissionsSoapClient service = new FunctionAndPermissionsSoapClient();
            ResultHelperOfDataTable serviceResult = service.getdeplist(departmentType);
            if (serviceResult.S)
            {
                dt = serviceResult.D as DataTable;
                string code = WebUtils.GetString("code");
                if (code != "")
                {
                    var d = dt.AsEnumerable().Where(x => x["DepartmentName"].ToString().Contains(code));
                    if (d.Count() > 0)
                    {
                        dt = d.CopyToDataTable<DataRow>();
                    }
                }

            }
            if (isAll == 1)
            {
                if (dt == null)
                {
                    dt.Columns.Add("DepartmentCode");
                    dt.Columns.Add("DepartmentName");
                }
                DataRow dr = dt.NewRow();
                dr["DepartmentCode"] = -1;
                dr["DepartmentName"] = "全部";
                dt.Rows.InsertAt(dr, 0);
            }

            if (isAddRows == 0)
            {
                return JsonDate(dt);
            }
            else
            {
                return JsonDate(new { Rows = dt });
            }
        }

        /// <summary>
        /// 获取一级部门
        /// </summary>
        /// <param name="isAddRows"></param>
        /// <param name="departmentType"></param>
        /// <returns></returns>
        public ActionResult GetParentDepartment(string departmentType = "")
        {
            DataTable dt = null;
            FunctionAndPermissionsSoapClient service = new FunctionAndPermissionsSoapClient();
            ResultHelperOfDataTable serviceResult = service.getdeplist(departmentType);
            if (serviceResult.S)
            {
                dt = serviceResult.D as DataTable;
                var d = dt.AsEnumerable().Where(x => DataConvert.StrToInt(x["ParentId"].ToString()) == 0);
                if (d.Count() > 0)
                {
                    dt = d.CopyToDataTable<DataRow>();
                }
                else
                {
                    dt = new DataTable();
                }
            }

            return JsonDate(dt);

        }
    }
}